/**
 * Copyright (C) 2009 Noel Ang
 *
 * This file is part of Samsara.
 *
 * Samsara is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Samsara is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Samsara.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package com.lunarshades.samsara.ui;

import java.awt.Component;
import java.awt.Dimension;
import java.util.Collection;
import java.util.StringTokenizer;

/**
 * Utility functions.
 *
 * @author Noel Ang, sparta.variable@gmail.com
 */
public class Utils {

private Utils() {
}

/**
 * Sets the preferred size of all the components equal to that of the largest
 * component's among them.
 *
 * @param components Set of components whose size is to be equalized.
 */
public static void equalizePreferredSizes(Collection<Component> components) {
    int width = 0;
    int height = 0;
    for (Component c : components) {
        Dimension dim = c.getPreferredSize();
        height = Math.max(height, dim.height);
        width = Math.max(width, dim.width);
    }

    final Dimension newDimension = new Dimension(width, height);
    for (Component c : components) {
        c.setPreferredSize(newDimension);
    }
}

/**
 * Transforms "camel hump" words into separate words, and capitalizes the first
 * character of each word.
 *
 * @param str The word or phrase to capitalize
 *
 * @return The capitalized result
 */
public static String toWords(String str) {
    StringTokenizer tokenizer = new StringTokenizer(str, " ", true);
    StringBuffer transformBuffer = new StringBuffer(str.length());
    while (tokenizer.hasMoreTokens()) {
        String token = tokenizer.nextToken();
        for (int i = 0, l = token.length(); i < l; ++i) {
            if (i == 0) {
                if (transformBuffer.length() == 0) {
                    transformBuffer.append(Character.toUpperCase(token.charAt(i)));
                } else {
                    transformBuffer.append(' ').append(Character.toUpperCase(
                            token.charAt(i)
                    )
                    );
                }
            } else {
                if (Character.isUpperCase(token.charAt(i))) {
                    transformBuffer.append(' ').append(token.charAt(i));
                } else {
                    transformBuffer.append(token.charAt(i));
                }
            }
        }
    }
    return transformBuffer.toString();
}

/**
 * Capitalizes the first letter in a string.
 *
 * @param str The string to capitalized
 *
 * @return A new string identical to str but with the first letter capitalized,
 *         or str if the string cannot be capitalized.
 */
public static String capitalize(String str) {
    if (str != null && !"".equals(str)) {
        char ch = str.charAt(0);
        if (!Character.isUpperCase(ch) && Character.isLetter(ch)) {
            StringBuilder sb = new StringBuilder(str);
            sb.replace(0,
                    1,
                    Character.toString(Character.toUpperCase(ch)).intern()
            );
            str = sb.toString().intern();
        }
    }
    return str;
}

public static boolean isNumberText(String str) {
    for (int i = 0, j = str.length(); i < j; ++i) {
        if (!Character.isDigit(str.charAt(i))) {
            return false;
        }
    }
    return true;
}

public static int makeInt(Object value) throws NumberFormatException {
    int aInt = 0;

    if (value != null) {
        value = value.toString().trim();
        if (!"".equals(value)) {
            StringBuilder sb = new StringBuilder(value.toString());

            // skip leading cruft
            int start = 0;
            for (;
                 start < sb.length() && !Character.isDigit(sb.charAt(start));
                 ++start) {
                ;
            }

            // lop off trailing cruft
            int end = start;
            for (;
                 end < sb.length() && Character.isDigit(sb.charAt(end));
                 ++end) {
                ;
            }

            if (start != end) {
                aInt = Integer.parseInt(sb.substring(start, end));
            } else {
                throw new NumberFormatException(value.toString());
            }
        }
    }
    return aInt;
}
}
